//
//  FRPGalleryViewController.m
//  FunctionalReactivePixels
//
//  Created by sun on 15/7/1.
//  Copyright (c) 2015年 sunfei. All rights reserved.
//

#import "FRPGalleryViewController.h"
#import "FRPGalleryFlowLayout.h"
#import "FRPCell.h"
#import <ReactiveCocoa/ReactiveCocoa.h>
#import <RACEXTScope.h>
#import <RACDelegateProxy.h>
#import "FRPPhotoImporter.h"
#import "FRPFullSizePhotoViewController.h"

#import "FRPFullSizePhotoViewModel.h"
#import "FRPGalleryViewModel.h"

static NSString *CellIdentifier = @"Cell";

@interface FRPGalleryViewController () // <FRPFullSizePhotoViewControllerDelegate>

@property (nonatomic, strong) NSArray *photoArray;
@property (nonatomic, strong) id collectionViewDelegate;
@property (nonatomic, strong) FRPGalleryViewModel *viewModel;

@end

@implementation FRPGalleryViewController

- (instancetype)init {
    FRPGalleryFlowLayout *flowLayout = [[FRPGalleryFlowLayout alloc] init];
    self = [super initWithCollectionViewLayout:flowLayout];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"Popular on 500px";
    
    self.viewModel = [[FRPGalleryViewModel alloc] init];
    [self.collectionView registerClass:[FRPCell class] forCellWithReuseIdentifier:CellIdentifier];
    
    @weakify(self)
    RAC(self, photoArray) = [[[[FRPPhotoImporter importPhotos] doCompleted:^{
        @strongify(self)
        [self.collectionView reloadData];
    }] logError] catchTo:[RACSignal empty]];
    
    RACDelegateProxy *viewControllerDelegate = [[RACDelegateProxy alloc] initWithProtocol:@protocol(FRPFullSizePhotoViewControllerDelegate)];
    [[viewControllerDelegate rac_signalForSelector:@selector(userDidScroll:toPhotoAtIndex:)]
     subscribeNext:^(RACTuple *value) {
         @strongify(self)
         [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:[value.second integerValue] inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:NO];
     }];
    
    self.collectionViewDelegate = [[RACDelegateProxy alloc] initWithProtocol:@protocol(UICollectionViewDelegate)];
    [[self.collectionViewDelegate rac_signalForSelector:@selector(collectionView:didSelectItemAtIndexPath:)] subscribeNext:^(RACTuple *arguments) {
        @strongify(self)
//        FRPFullSizePhotoViewController *viewController = [[FRPFullSizePhotoViewController alloc] initWithPhotoModels:self.photoArray currentPhotoIndex:[(NSIndexPath *)arguments.second item]];
        NSIndexPath *indexPath = arguments.second;
        FRPFullSizePhotoViewModel *viewModel = [[FRPFullSizePhotoViewModel alloc] initWithPhotoArray:self.viewModel.model initialPhotoIndex:[indexPath item]];
        FRPFullSizePhotoViewController *viewController = [[FRPFullSizePhotoViewController alloc] init];
        viewController.viewModel = viewModel;
        viewController.delegate = (id<FRPFullSizePhotoViewControllerDelegate>)viewControllerDelegate;
        [self.navigationController pushViewController:viewController animated:YES];
    }];
    // 原著中丢掉了这句话，导致函数不会执行,而且不需要再扩展中实现FRPFullSizePhotoViewControllerDelegate协议了
    // 如果不是用RACDelegateProxy而使用self的这个函数，需要实现这个协议并且实现默认的空方法。当然，这时也不需要下面这句话
    self.collectionView.delegate = self.collectionViewDelegate;
}

//- (void)loadPopularPhotos {
//    @weakify(self)
//    [[FRPPhotoImporter importPhotos] subscribeNext:^(id x) {
//        @strongify(self)
//        self.photoArray = x;
//    } error:^(NSError *error) {
//        NSLog(@"cannot fetach photots from 500px:%@", error);
//    }];
//}

#pragma mark - UICollectionViewDatasourse Methods
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.photoArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    FRPCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier
                                                              forIndexPath:indexPath];
    [cell setPhotoModel:self.photoArray[indexPath.row]];
    return cell;
}

@end
